package code.main.view.table
{
	import code.common.assets.AssetsClassName;
	import code.main.controller.table.SeatCtrl;
	import code.main.modle.table.Player;
	import code.module.sound.SoundManager;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Circ;
	import com.yy.manager.ResourceManager;
	import com.yy.tools.graphics.GT;
	import com.yy.tools.load.LoaderPlus;
	import com.yy.ui.controls.StateButton;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.text.TextField;

	/**
	 *桌子面板 
	 * @author YY
	 * 
	 */
	public class TableView extends Sprite
	{
		private var offsetY:int = 0;//y坐标偏移
		
		public var tableView:MovieClip;
		public var goHallBtn:SimpleButton;
		public var upSeatBtn:SimpleButton;
		public var level:TextField;
		public var totalMoney:TextField;
		public var addBetUi:MovieClip;//下注资源
		public var timeUi:MovieClip;//倒计时
		public var clock:Clock;//闹钟
		public var grabBankPanel:MovieClip;
		public var grabBankBtn:PlayBtn;
		public var giveUpBankBtn:PlayBtn;
		public var soundSwitchBtn:StateButton;//声音开关
		public var buttonUi:MovieClip;//底部资源
		
		private var _uiPos:Array = [
			[[343,153],],
			[[557,201],[530,83],[604,83],[543,72],[696,96]],
			[[630,319],[609,221],[683,221],[634,230],[595,256]],
			[[557,434],[530,394],[604,394],[543,396],[696,419]],
			[[385,492],[310,458],[385,458],[324,464],[479,486]],
			[[195,434],[68,394],[142,394],[95,396],[253,419]],
			[[124,319],[0,221],[74,221],[3,230],[160,256]],
			[[195,201],[68,83],[142,83],[95,72],[253,96]],
		];//装logo的坐标 [[庄],[抢庄],[放弃],[下注],[亮牌]]
		public function TableView()
		{
			var oldX:int;
			var oldY:int;
			var dobj:DisplayObject;
			tableView = ResourceManager.getMovieClipByName(AssetsClassName.TABLE_VIEW_UI);
			GT.stopMovie(tableView);
			dobj = addChild(tableView);
			dobj.y = offsetY;
			buttonUi = tableView["buttonUi"];
			buttonUi.y -= offsetY;
			grabBankPanel = tableView["grabBankerPanel"];
			grabBankBtn = new PlayBtn(tableView["grabBankerPanel"]["grabBankerBtn"]);
			grabBankPanel.addChild(grabBankBtn);
			giveUpBankBtn = new PlayBtn(tableView["grabBankerPanel"]["giveUpBankerBtn"]);
			grabBankPanel.addChild(giveUpBankBtn);
			
			goHallBtn = tableView.goHallBtn;
			upSeatBtn = tableView.upSeatBtn;
			addBetUi = tableView["addBetUi"];
			timeUi = tableView["timeUi"];
			
//			oldX = tableView["soundBtn"].x;
//			oldY = tableView["soundBtn"].y;
//			tableView["soundBtn"].x = 0;
//			tableView["soundBtn"].y = 0;
			soundSwitchBtn = new StateButton(buttonUi["soundBtn"]);
			soundSwitchBtn.tween = true;
			dobj = buttonUi.addChild(soundSwitchBtn);
//			dobj.x = oldX;
//			dobj.y = oldY;
			soundSwitchBtn.select = SoundManager.SOUND_SWITCH;
			clock = new Clock();
			addChild(clock);
			clock.x = 360;
			clock.y = 241;
			resetTableView();
		}

		/**
		 *设置操作ui的坐标 
		 * @param seatId
		 * 
		 */
		public function setOptionPos(seatId:int):void {
			var arr:Array = _uiPos[seatId];
			var arr2:Array = ["showCardBtn"];
			var i:int;
			var len:int = arr2.length;
			for(i=0; i<len; i++) {
				tableView[arr2[i]].x = arr[4][0];
				tableView[arr2[i]].y = arr[4][1];
			}
		}
		
		/**
		 *重置桌子资源 
		 * 
		 */
		public function resetTableView():void {
			//隐藏座位信息
			var i:int;
			var len:int = 7;
			var lp:LoaderPlus;
			for(i=0; i<len; i++) {
				//牌
				tableView["card"+i].visible = false;
				//隐藏的牌
				tableView["hideCard" + i].visible = false;
				//用户信息
				tableView["user"+i].visible = false;
				lp = new LoaderPlus();
				tableView["user"+i].userPic.img = lp;
				tableView["user"+i].userPic.addChild(lp);
				//坐下按钮
				tableView["user"+i].effect.gotoAndStop("space");
				//牛值
				tableView["cowNumb"+i].visible = false;
			}
//			tableView["addBetUi"].visible = false;
			setGrabUiVisible(false);
			setShowCardBtnVisible(false);
		}
		
		/**
		 *播放坐下效果 
		 * @param seatId
		 * 
		 */
		public function playerSeatEffect(seatId:int):void {
			seatId--;
			tableView["user"+seatId].effect.play();
		}
		
		/**
		 *显示用户图片 
		 * @param pic
		 * 
		 */
		public function showSeatImg(seatId:int, pic:String):void {
			seatId--;
			var lp:LoaderPlus = tableView["user"+seatId].userPic.img;
			lp.load(new URLRequest(pic));
			lp.width = 48;
			lp.height = 48;
		}
		
		/**
		 *设置桌面金币 
		 * 
		 */
		public function setMoney(seatId:int,money:int):void {
			seatId--;
			tableView["user"+seatId].money.text = money;
		}
		
		/**
		 *设置用户名 
		 * 
		 */
		public function setUserName(seatId:int,userName:String):void {
			seatId--;
			tableView["user"+seatId].userName.text = String(userName);
		}
		
		/**
		 *设置称号 
		 * @param seatId
		 * @param titleName
		 * 
		 */
		public function setTitleName(seatId:int,titleName:String):void {
			seatId--;
//			tableView["user"+seatId].title.text = String(titleName);
		}
		
		
		/**
		 *设置牛 
		 * 
		 * @param seatId 座位id
		 * @param visible 是否可见
		 * @param count 牛值
		 * 
		 */
		public function setCowNumb(seatId:int, visible:Boolean, count:int=0):void {
			seatId--;
			var mc:MovieClip = tableView["cowNumb"+seatId];
			mc.visible = visible;
			if(visible) {
				mc.gotoAndStop(count);
			}
		}
		
		/**
		 *设置一个位置坐下按钮是否可见 
		 * @param seatId
		 * @param visible
		 * 
		 */
		public function setSeatBtnVisible(seatId:int, visible:Boolean):void {
			seatId--;
			tableView["seatBtn"+seatId].visible = visible;
		}
		
		/**
		 *设置一个位置用户信息ui是否显示 
		 * @param seatId
		 * @param visible
		 * 
		 */
		public function setUserInfoUiVisible(seatId:int, visible:Boolean):void {
			seatId--;
			tableView["user"+seatId].visible = visible;
		}
		
		/**
		 *设置抢庄界面是否显示 
		 * @param boo
		 * 
		 */
		public function setGrabUiVisible(boo:Boolean):void {
			grabBankPanel.visible = boo;
		}
		
		/**
		 *设置亮牌按钮显示状况 
		 * @param boo
		 * 
		 */
		public function setShowCardBtnVisible(boo:Boolean):void {
			tableView["showCardBtn"].visible = boo;
		}
		
		
		/**
		 *显示玩家基本信息 
		 * @param player
		 * 
		 */
		public function showUserInfo(player:Player):void {
			showSeatImg(player.seatId, player.pic);
			setMoney(player.seatId, player.tableMoney);
			setUserName(player.seatId, player.userName);
			setTitleName(player.seatId, String(player.titleId));
			setSeatBtnVisible(player.seatId, false);
			setSeatVisible(player.seatId, true);
		}
		
		/**
		 *设置一个位置是否可见 
		 * 
		 */
		public function setSeatVisible(seatId:int, visible:Boolean):void {
			setUserInfoUiVisible(seatId, visible);
			if(visible) {
				setSeatBtnVisible(seatId, false);
			}else {
				setSeatBtnVisible(seatId, true);
				setCowNumb(seatId, false);
			}
		}
		
		
		/**
		 * 获得显示的牌
		 * 
		 */
		public function getShowCardMc(seatId:int):MovieClip {
			seatId--;
			var mc:MovieClip = tableView["card" + seatId];
			return mc;
		}
		
		
		/**
		 *获得隐藏的牌 
		 * @param seatId
		 * @return 
		 * 
		 */
		public function getHideCardMc(seatId:int):MovieClip {
			seatId--;
			var mc:MovieClip = tableView["hideCard" + seatId];
			return mc;
		}
		
		/**
		 *重设 装ui 
		 * 
		 */
		public function resetBankerLogo():void {
			var pos:Array = _uiPos[0][0];
			var ui:DisplayObject = tableView["publicBankPic"];
			if(ui.x == pos[0] && ui.y == pos[1]) {
				
			}else {
				TweenLite.to(ui, .5, {x:pos[0], y:pos[1], ease:Circ.easeOut});
			}
		}
		
		/**
		 *设置庄家的logo 
		 * 
		 */
		public function setBankerLogo(seatId:int):void {
			var pos:Array = _uiPos[seatId][0];
			var ui:DisplayObject = tableView["publicBankPic"];
			if(ui.x == pos[0] && ui.y == pos[1]) {
				
			}else {
				TweenLite.to(ui, .5, {x:pos[0], y:pos[1], ease:Circ.easeOut});
			}
		}
	}
}